home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / wais / waisgate / macbuild.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  8KB  |  290 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  
  5.    Brewster@think.com
  6. */
  7.  
  8. /* Change log:
  9.  * $Log:    macbuild.c,v $
  10.  * Revision 1.2  92/02/12  13:35:04  jonathan
  11.  * Added "$Log: $" so RCS will put the log message in the header
  12.  * 
  13. */
  14.  
  15. /* This is a clumsy index building program for the Mac.
  16.  * For some reason all this Class garbage is needed to
  17.  * set up something to call the file routines.
  18.  * Sorry about the novice mac code.
  19.  *
  20.  * -brewster 7/90
  21.  */
  22.  
  23. #include "irfiles.h"
  24. #include "irhash.h"
  25. #include "irtfiles.h"
  26. #include "panic.h"
  27. #include "cutil.h"
  28. #include "irext.h"
  29.  
  30. #include <profile.h>
  31. #include <EventMgr.h>
  32. #include <FileMgr.h>
  33.  
  34. /*----------------------------------------------------------------------*/
  35. /* Class definitions */
  36.  
  37. #include <CApplication.h>
  38. #include <CFile.h>
  39. #include <CView.h>
  40. #include <CWindow.h>
  41. #include <CDesktop.h>
  42. #include <CDirector.h>
  43. #include "CDynamicError.h"
  44.  
  45. extern    CApplication    *gApplication;
  46.  
  47. struct CBuildApp : CApplication 
  48. {
  49.   void    IBuildApp(void);
  50. };
  51.  
  52. void CBuildApp::IBuildApp(void)
  53. {
  54.   CApplication::IApplication(4, 20480L, 2048L);
  55.   gError->Dispose(); /* get rid if the CError that IApplication defined */
  56.   gError = new(CDynamicError); /* make our own error handler */
  57. }
  58.  
  59. /*----------------------------------------------------------------------*/
  60. /* Gets a full filename from the mac file objects.
  61.  * getwd was taken from utils.c from the 
  62.  * document retrieval system.
  63.  */
  64.  
  65. #include <HFS.H>
  66. #include <string.h>
  67. /* for file manipulation routines */
  68. typedef enum file_style {MAC_FILE_STYLE, UNIX_FILE_STYLE} file_style;
  69.  
  70. static char 
  71. *getwd(file_style style)
  72. /* This function returns the unix style path name which is set by any of the SF
  73.    routines (using the global vars).  The code is from the net, I have no idea
  74.    who the author might be.  He/She does include the following note:
  75.    
  76.           Note that it is perfectly legal for a Macintosh owner to create a
  77.         directory hierarchy where the length of full path names of the deepest
  78.         files exceeds 255 bytes; since the file system never manipulates full
  79.         pathnames internally (it only ever sees them when passed as parameters),
  80.         it doesn't and needn't check.  However, this poses an ethical problem if
  81.         you are constructing full pathnames: my code simply bombs if it would
  82.         construct a pathname >255 bytes, and if I increased the buffer size, the
  83.         resulting pathnames are useless except for documentation purposes (since
  84.         the file system can't have string parameters >255 bytes).
  85.  */
  86. {
  87.     CInfoPBRec d;
  88.     static char ret[255];
  89.     char nm[50], tmp[255];
  90.     long cur_dir = CurDirStore; /* mac global var */
  91.     long cur_vol = 0 - SFSaveDisk; /* mac global var */
  92.     
  93.     ret[0] = '\0';
  94.     d.dirInfo.ioDrDirID = cur_dir;
  95.     for(;;) {
  96.         d.dirInfo.ioCompletion = 0;
  97.         d.dirInfo.ioNamePtr = (StringPtr) nm;
  98.         d.dirInfo.ioVRefNum = cur_vol;
  99.         d.dirInfo.ioFDirIndex = -1;
  100.  
  101.         PBGetCatInfo(&d,0);
  102. /*        if(d.ioResult != noErr) return(0); this is not defined in lightspeed's headers */
  103.         PtoCstr((char *) nm);
  104.         strcpy(tmp,ret);
  105.         if (style == UNIX_FILE_STYLE)
  106.           strcpy(ret,"/");
  107.         else 
  108.           strcpy(ret,":");
  109.         strcat(ret,nm);
  110.         strcat(ret,tmp);
  111.         if(d.dirInfo.ioDrDirID == 2) break;    /* home directory */
  112.         d.dirInfo.ioDrDirID = d.dirInfo.ioDrParID;
  113.     } 
  114.     /* if its MAC style, remove the leading colon */
  115.     if (style == MAC_FILE_STYLE)
  116.       return(ret+1);
  117.     else
  118.       return(ret);
  119. }
  120.  
  121. /*----------------------------------------------------------------------*/
  122. /* Gets a filename from the user (one that exists already) */
  123.  
  124. static boolean 
  125. get_filename(char* prompt, char *filename)
  126. {
  127.    SFReply    macSFReply;
  128.    Point pos;
  129.    pos.h = pos.v = 100;
  130.    SFGetFile(pos, (StringPtr)"\pFile to Index:", 
  131.              NULL, -1, NULL, NULL, &macSFReply);
  132.    if(macSFReply.good)
  133.     { /* then we have a gotten a good file.
  134.        * put together the full filename
  135.        */
  136.          
  137.       strcpy(filename, getwd(MAC_FILE_STYLE));
  138.       strcat(filename, ":");
  139.       PtoCstr((char *)macSFReply.fName);
  140.       strcat(filename, (char *)macSFReply.fName);
  141.       CtoPstr((char *)macSFReply.fName);
  142.       return(true);
  143.      }
  144.     else
  145.       return(false);
  146. }
  147.  
  148. /*----------------------------------------------------------------------*/
  149. /* Gets a filename from the user (one that does not exist already) */
  150.  
  151. static int get_new_filename(char *filename)
  152. {
  153.     SFReply    macSFReply;
  154.     Point pos;
  155.     pos.h = pos.v = 100;
  156.     
  157.     SFPutFile(pos, (StringPtr)"\pLocation of Index:", 
  158.                    (StringPtr)"\pindex", 
  159.               NULL, &macSFReply);
  160.     if(macSFReply.good){
  161.         /* then we have a gotten a good file.
  162.          * put together the full filename
  163.          */
  164.          
  165.          strcpy(filename, getwd(MAC_FILE_STYLE));
  166.          strcat(filename, ":");
  167.          PtoCstr((char *)macSFReply.fName);
  168.          strcat(filename, (char *)macSFReply.fName);
  169.          CtoPstr((char *)macSFReply.fName);
  170.          return(true);
  171.     }
  172.     else{
  173.     return(false);
  174.     }
  175. }
  176.  
  177. /*----------------------------------------------------------------------*/
  178. /* Main routine for building an index */
  179.  
  180. FILE *logfile = NULL;
  181.  
  182. void main()
  183. {    
  184.   char filename[MAX_FILE_NAME_LEN + 1]; 
  185.   char index_filename[MAX_FILE_NAME_LEN + 1];
  186.   database* db;
  187.   long count;
  188.   SFReply macSFReply;
  189.   Point pos;
  190.  
  191.   InitProfile(3000,200);
  192.     
  193. _profile = false;
  194. _trace = false;
  195. freopen("stdout","w",stdout);
  196.     
  197.   pos.h = pos.v = 100;
  198.  
  199.   gApplication = new(CBuildApp);
  200.   ((CBuildApp *)gApplication)->IBuildApp();
  201.   
  202.   if(true == get_new_filename(index_filename))
  203.     printf("The full index filename is: %s\n", index_filename);
  204.   else 
  205.     return;
  206.     
  207.   printf("Initializing Database.\n");
  208.   db = openDatabase(index_filename, true,false);
  209.   if (db == NULL)
  210.     panic("unable to open the database\n");
  211.   db->the_word_memory_hashtable =
  212.     init_word_memory_hashtable(65536L, 500000L, db->the_word_memory_hashtable);
  213.  
  214.       SFGetFile(pos, (StringPtr)"\pFiles to Index:", 
  215.               NULL, -1, NULL, NULL, &macSFReply);
  216.     if(macSFReply.good)
  217.     {
  218.         /* then we have a file.  In this version we index all the
  219.          * files in the volume of the selected file 
  220.          */
  221.          OSErr Error;
  222.          CInfoPBRec PBRec;
  223.          CInfoPBPtr PBPtr = &PBRec;
  224.          
  225.          char nm[50];
  226.          char dirname[500];
  227.  
  228. _profile = true;
  229.  
  230.          strcpy(dirname, getwd(MAC_FILE_STYLE));
  231.          
  232.          /* set up the PBRec */
  233.          PBPtr->hFileInfo.ioVRefNum  = 0 - SFSaveDisk;
  234.          PBPtr->hFileInfo.ioDirID = CurDirStore;
  235.          PBPtr->hFileInfo.ioNamePtr = macSFReply.fName;
  236.          PBPtr->hFileInfo.ioCompletion = NULL;
  237.          PBPtr->hFileInfo.ioFDirIndex = 0; 
  238.          Error = PBGetCatInfo(PBPtr, FALSE); 
  239.          
  240.          PBPtr->hFileInfo.ioVRefNum  = 0 - SFSaveDisk;
  241.          PBPtr->hFileInfo.ioDirID = CurDirStore;
  242.          PBPtr->hFileInfo.ioFDirIndex = 1; 
  243.          PBPtr->hFileInfo.ioNamePtr = (StringPtr) nm;
  244.          while((Error = PBGetCatInfo(PBPtr, FALSE)) != fnfErr)
  245.          {
  246.              /* loop until we are done with the volume */
  247.              PBPtr->hFileInfo.ioVRefNum  = 0 - SFSaveDisk;
  248.              PBPtr->hFileInfo.ioDirID = CurDirStore;
  249.              PBPtr->hFileInfo.ioNamePtr = (StringPtr) nm;
  250.              PBPtr->hFileInfo.ioCompletion = NULL;
  251.             PBPtr->hFileInfo.ioFDirIndex++;
  252.             /* to determine if a record is a directory do
  253.              * PBPtr->hFileInfo.ioFlAttrib & 0x10
  254.              */
  255.             
  256.             strcpy(filename, dirname);
  257.             strcat(filename, ":");
  258.             PtoCstr(nm);
  259.             strcat(filename, nm);
  260.             CtoPstr(nm);
  261.             if(!(PBPtr->hFileInfo.ioFlAttrib & 0x10))
  262.             {
  263.                 printf("Indexing File: %s...\n", filename);
  264.                 index_text_file(filename, 
  265.                         NULL, NULL, NULL, 
  266.                         "TEXT", /* this should be the mac filetype XXX */
  267.                         db,true, false);
  268.             }
  269.             else
  270.                 printf("Directory %s is not indexed\n", filename);
  271.          }
  272.          
  273. _profile = false;
  274.  
  275.     }
  276.  
  277.     printf("Finished entering Files to be indexed.\n");
  278.     printf("Now updating disk files\n");
  279.      finished_add_word(db);
  280.       closeDatabase(db);
  281.     
  282.     printf("Finished with the indexing. Exiting.\n");
  283.     for(count = 0; count < 100000; count++)
  284.       ;  /* delay to show message */
  285.       
  286.     gApplication->Exit();
  287. }
  288.  
  289. /*----------------------------------------------------------------------*/
  290.